package service.impl;

import bean.vo.*;
import commons.xlhUtils.MyFormatUtils;
import dao.EchartsDao;
import dao.impl.EchartsDaoImpl;
import service.EchartsService;

import java.math.BigDecimal;
import java.util.*;

/**
 * service.impl
 * User: 邱
 * Date: 2024/12/16 18:41
 * Description:
 * Version: V1.0
 */
public class EchartsServiceImpl implements EchartsService {
    EchartsDao echartsDao = new EchartsDaoImpl();

    /**
     * 将 MonthDataVo 中的每月数据转化成 list 形式
     * @param firstData
     * @return
     */
    private List<BigDecimal> getMonthDataVo(MonthDataVo firstData) {
        List<BigDecimal> first = new Vector<>();
        first.add(firstData.getJanuary());first.add(firstData.getFebruary());first.add(firstData.getMarch());first.add(firstData.getApril());first.add(firstData.getMay());first.add(firstData.getJune());
        first.add(firstData.getJuly());first.add(firstData.getAugust());first.add(firstData.getSeptember());first.add(firstData.getOctober());first.add(firstData.getNovember());first.add(firstData.getDecember());

        return first;
    }

    /**
     * 查询缴费情况
     * @return
     */
    @Override
    public MonthDataVo findChargeList() {
        //获取今年年份
        String date = MyFormatUtils.toString(new Date(), "yyyy");
        //获取今年月份
        List<String> listDate = new Vector<>();
        for (int i = 1; i <= 12; i++) {
            listDate.add(date+"-"+i);
        }
        listDate.add(date);

        List<MonthDataVo> list = echartsDao.findChargeList(listDate);
        MonthDataVo firstData = list.get(0);
        MonthDataVo endData = list.get(1);
        MonthDataVo tempData = new MonthDataVo();
        //获取每月的数据
        if (firstData.getIsDunned() == 0){
            List<BigDecimal> zeroMonthData = getMonthDataVo(firstData);
            tempData.setZeroMonthData(zeroMonthData);

            List<BigDecimal> oneMonthData = getMonthDataVo(endData);
            tempData.setOneMonthData(oneMonthData);
        }else {
            List<BigDecimal> zeroMonthData = getMonthDataVo(endData);
            tempData.setZeroMonthData(zeroMonthData);

            List<BigDecimal> oneMonthData = getMonthDataVo(firstData);
            tempData.setOneMonthData(oneMonthData);
        }
        return tempData;
    }

    /**
     * 查询访客情况
     * @return
     */
    @Override
    public MonthDataVo findVisitorList() {
        MonthDataVo firstData = echartsDao.findVisitorList();

        List<BigDecimal> oneMonthData = getMonthDataVo(firstData);
        firstData.setOneMonthData(oneMonthData);
        return firstData;
    }

    /**
     * 查询投诉情况
     * @return
     */
    @Override
    public MonthDataVo findComplaintList() {
        MonthDataVo firstData = echartsDao.findComplaintList();

        List<BigDecimal> oneMonthData = getMonthDataVo(firstData);
        firstData.setOneMonthData(oneMonthData);
        return firstData;
    }

    /**
     * 查询售卖情况
     * @return
     */
    @Override
    public MonthDataVo findSaleGoodsList() {
        MonthDataVo firstData = echartsDao.findSaleGoodsList();

        List<BigDecimal> oneMonthData = getMonthDataVo(firstData);
        firstData.setOneMonthData(oneMonthData);
        return firstData;
    }

    /**
     * 查询费用类型及其对应费用
     * @return
     */
    @Override
    public Map<String, List> findChargeRecord() {
        //获取今年年份
        String date = MyFormatUtils.toString(new Date(), "yyyy");
        List<PaymentVo> paymentList = echartsDao.findChargeRecord(date);
        // 通过Stream API获取最大值
        Optional<BigDecimal> maxPaymentOptional = paymentList.stream()
                .map(PaymentVo::getPayment)
                .max(BigDecimal::compareTo);
        BigDecimal maxPayment = maxPaymentOptional.get().add(new BigDecimal("1000"));
        //用来存费用类型和最大值
        List<Map<String, Object>> typeList = new Vector<>();

        Map<String, List> map = new HashMap<>();
        //用来存已缴费和未缴费的费用类型对应的值
        List<BigDecimal> valueNot = new Vector<>();
        List<BigDecimal> valueHas = new Vector<>();
        List<Map<String, Object>> valueList = new Vector<>();
        Map<String, Object> valueNotInfo = new HashMap<>();
        Map<String, Object> valueHasInfo = new HashMap<>();

        paymentList.forEach( a -> {
            if (a.getIsDunned() == 0){
                Map<String, Object> typeInfo = new HashMap<>();
                typeInfo.put("name", a.getTypeName());
                typeInfo.put("max", maxPayment);
                typeList.add(typeInfo);
                valueNot.add(a.getPayment());
            }else {
                valueHas.add(a.getPayment());
            }
        });
        valueNotInfo.put("value", valueNot);
        valueNotInfo.put("name", "未缴费");
        valueHasInfo.put("value", valueHas);
        valueHasInfo.put("name", "已缴费");
        valueList.add(valueNotInfo);
        valueList.add(valueHasInfo);

        map.put("typeInfo", typeList);
        map.put("valueInfo", valueList);

        return map;
    }

    @Override
    public Map<String, List> findMaintenance() {
        List<MaintenanceVo> MaintenanceList = echartsDao.findMaintenance();
        List<String> item = new Vector<>();
        List<Long> value = new Vector<>();
        Map<String, List> map = new HashMap<>();
        MaintenanceList.forEach( a -> {
            item.add(a.getMaintenanceItem());
            value.add(a.getMaintenanceNum());
        });
        map.put("maintenanceList", item);
        map.put("valueList", value);
        return map;
    }

    /**
     * 查询医疗预约情况
     * @return
     */
    @Override
    public Map<String, List> findMedicalList() {
        List<AppointmentVo> medicalList = echartsDao.findMedicalList();
        List<String> item = new Vector<>();
        List<Map> value = new Vector<>();
        Map<String, List> map = new HashMap<>();
        medicalList.forEach( a -> {
            Map temp = new HashMap<>();
            item.add(a.getMedicalItem());
            temp.put("value", a.getValue());temp.put("name", a.getMedicalItem());
            value.add(temp);
        });
        map.put("medicalItemList", item);
        map.put("valueList", value);
        return map;
    }

    /**
     * 查询访客人数
     * @return
     */
    @Override
    public VisitorNumVo findVisitorNumber() {
        return echartsDao.findVisitorNumber();
    }

    @Override
    public DwellerNumVo findAllOwner() {
        return echartsDao.findAllOwner();
    }

    @Override
    public DwellerNumVo findAllDweller() {
        return echartsDao.findAllDweller();
    }

    @Override
    public PaymentVo findPayment() {
        return echartsDao.findPayment();
    }

    @Override
    public OrderInfoVo findSaleGoodsNum() {
        return echartsDao.findSaleGoodsNum();
    }

    @Override
    public DwellerNumVo findComplaintNum() {
        return echartsDao.findComplaintNum();
    }
}
